$(function() {
    var pathName=document.location.pathname;
    console.log("pathName="+pathName);
    var index=pathName.substr(1).indexOf("/");
    var result=pathName.substr(0,index+1);
    console.log(result);
    var flag = 0;

    $(".password img").click(function() {
        if (flag == 0){
            $(".password .inp").attr("type","text");
            $(this).attr("src","images/open.png");
            flag = 1;
        } else {
            $(".password .inp").attr("type","password");
            $(this).attr("src","images/close.png");
            flag = 0;
        }
    });
    $(".repassword img").click(function() {
        if (flag == 0){
            $(".repassword .inp").attr("type","text");
            $(this).attr("src","images/open.png");
            flag = 1;
        } else {
            $(".repassword .inp").attr("type","password");
            $(this).attr("src","images/close.png");
            flag = 0;
        }
    });

    $(".reg_form #tel").blur(function() {
        if ($(this).val().length == 0) {
            $(this).siblings("span").attr("class","hint");
            $(this).siblings("span").children("i").attr("class","hint_icon");
            $(this).siblings("span").children("em").text("请输入11位手机号码!");
        } else {
            if ($(this).val().length != 11) {
                $(this).siblings("span").attr("class","error");
                $(this).siblings("span").children("i").attr("class","error_icon");
                $(this).siblings("span").children("em").text("手机号码格式不正确，请重新输入!");
            } else {
                $(this).siblings("span").attr("class","success");
                $(this).siblings("span").children("i").attr("class","success_icon");
                $(this).siblings("span").children("em").text("手机号码格式正确!");
            }
        }
    });

    $(".reg_form #psw").blur(function() {
        if ($(this).val().length == 0) {
            $(this).siblings("span").attr("class","hint");
            $(this).siblings("span").children("i").attr("class","hint_icon");
            $(this).siblings("span").children("em").text("请输入6~16位密码!");
        } else {
            if ($(this).val().length < 6 || $(this).val().length > 16) {
                $(this).siblings("span").attr("class","error");
                $(this).siblings("span").children("i").attr("class","error_icon");
                $(this).siblings("span").children("em").text("密码格式不正确，请重新输入!");
            } else {
                $(this).siblings("span").attr("class","success");
                $(this).siblings("span").children("i").attr("class","success_icon");
                $(this).siblings("span").children("em").text("密码格式正确!");
            }
        }
    });

    $(".reg_form #repsw").blur(function() {
        if ($(this).val().length == 0) {
            $(this).siblings("span").attr("class","hint");
            $(this).siblings("span").children("i").attr("class","hint_icon");
            $(this).siblings("span").children("em").text("请再次输入密码!");
        } else {
            if ($(this).val() != $(".reg_form #psw").val()) {
                $(this).siblings("span").attr("class","error");
                $(this).siblings("span").children("i").attr("class","error_icon");
                $(this).siblings("span").children("em").text("密码前后不一致，请重新输入!");
            } else {
                $(this).siblings("span").attr("class","success");
                $(this).siblings("span").children("i").attr("class","success_icon");
                $(this).siblings("span").children("em").text("密码确认完毕!");
            }
        }
    });
    $("form").submit(function() {
        if ($("#tel").val() == '') {
            alert("手机号码不能为空!");
        } else if ($("#code").val() == '') {
            alert("短信验证码不能为空!");
        } else if ($("#psw").val() == '') {
            alert("密码不能为空!");
        } else if ($("#repsw").val() == '') {
            alert("确认密码不能为空!");
        } else if (!$(".agree input[type='checkbox']").prop("checked")){
            alert("请先同意《花语花坊用户注册协议和隐私政策》协议。");
        }
        var member_name = $("#name").val();
        console.log("member_name="+member_name);
        var member_nickname = $("#name2").val();
        console.log("member_nickname="+member_nickname);
        var member_phone = $("#tel").val();
        console.log("member_phone="+member_phone);
        var member_pwd = $("#repsw").val();
        console.log("member_pwd="+member_pwd);
        var member_address = $("#address").val();
        console.log("member_address="+member_address);
        var member_email = $("#email").val();
        console.log("member_email="+member_email);
        var member_gender=$('input[name="form-field-radio"]:checked').val();
        console.log("member_gender="+member_gender);
        /*member_name, member_nickname, member_phone, member_pwd, member_address, member_email*/
        $.ajax({
            url:result+"/member/doregister",
            type:"POST",
            /*member_name, member_nickname, member_phone, member_pwd, member_address, member_email*/
            data:{"member_name":member_name,
                "member_nickname":member_nickname,
                "member_phone":member_phone,
                "member_pwd":member_pwd,
                "member_gender":member_gender,
                "member_address":member_address,
                "member_email":member_email,
            },
            dataType:"json",
            success:function (data) {
                if (data.success){
                    alert("注册成功!");
                }else {
                    alert("注册失败！！");
                }
            }
        })
    });
    /*$("form").submit(function() {
        if ($("#tel").val() == '') {
            alert("手机号码不能为空!");
        } else if ($("#code").val() == '') {
            alert("短信验证码不能为空!");
        } else if ($("#psw").val() == '') {
            alert("密码不能为空!");
        } else if ($("#repsw").val() == '') {
            alert("确认密码不能为空!");
        } else if (!$(".agree input[type='checkbox']").prop("checked")){
            alert("请先同意《花语花坊用户注册协议和隐私政策》协议。");
        } else {
            /!*$.ajax({
                url:result+"/member/doregister",
                type:"get",
                /!*member_name, member_nickname, member_phone, member_pwd, member_address, member_email*!/
                data:{"member_name":member_name,
                    "member_nickname":member_nickname,
                    "member_phone":member_phone,
                    "member_pwd":member_pwd,
                    "member_address":member_address,
                    "member_email":member_email,
                },
                dataType:"json",
                success:function (data) {
                    if (data.success){
                        alert("注册成功!");
                        window.location.href="${pageContext.request.contextPath}/member/login";
                    }else {
                        alert("注册失败！！");
                    }
                }
            })*!/
            //$("form").attr("action","login.jsp");
        }
    });*/
});


